/************************************************************************
*************************************************************************
@Name :       	QapTcha - jQuery Plugin
@Revison :    	2.5
@Date : 		26/01/2011
@Author:     	 Surrel Mickael (www.myjqueryplugins.com - www.msconcept.fr) 
@License :		 Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
 
**************************************************************************
*************************************************************************/
jQuery.QapTcha = {
	build : function(options)
	{
        var defaults = {
			txtLock : '滑动滚动条显示投票按钮!',
			txtUnlock : '请投出您宝贵的一票!'
        };   

		if(this.length>0)
		return jQuery(this).each(function(i) {
			/** Vars **/
			var opts = $.extend(defaults, options),
				$this = $(this),
				Clr = jQuery('<div>',{'class':'clr'}),
				bgSlider = jQuery('<div>',{'class':'bgSlider'}),
				Slider = jQuery('<div>',{'class':'Slider'}),
				Icons = jQuery('<div>',{'class':'Icons'}),
				TxtStatus = jQuery('<div>',{'class':'TxtStatus','class':'dropError',text:opts.txtLock}),
				inputQapTcha = jQuery('<input>',{name:'iQapTcha',value:'',type:'hidden'}),
			    qaptchaid = $this.attr("id");

			/** Construct DOM **/
			bgSlider.appendTo($this);
			Icons.insertAfter(bgSlider);
			TxtStatus.insertAfter(Icons);
			Clr.insertAfter(TxtStatus);
			inputQapTcha.appendTo($this);
			Slider.appendTo(bgSlider);
			$this.show();

			Slider.draggable({ 
				containment: bgSlider,
				axis:'x',
				stop: function(event,ui){
					if(ui.position.left > 119)
					{
						generatepass = generatePass();
						inputQapTcha.val(generatepass);

						 $.post("phpcms/templates/default/api/Qaptcha.jquery.php",
							   {
							      qaptchaid : qaptchaid,
							      generatepass:generatepass
							   },
							   function(data) {
							   	   info = eval(data);
							   	   if(info.state == 0)
									{
										Slider.draggable('disable').css('cursor','default');
										//inputQapTcha.val(generatePass());
										TxtStatus.text(opts.txtUnlock).addClass('dropSuccess').removeClass('dropError');
										Icons.css('background-position', '-16px 0');
										$this.prev(".vote").show();
									} else {
									  alert("投票系统出错了！请联系管理员。");
									}
								},
								'json'
						);
					
					}
				}
			});
			
			function generatePass() {
		        var chars = 'azertyupqsdfghjkmwxcvbn23456789AZERTYUPQSDFGHJKMWXCVBN';
		        var pass = '';
		        for(i=0;i<10;i++){
		            var wpos = Math.round(Math.random()*chars.length);
		            pass += chars.substring(wpos,wpos+1);
		        }
		        return pass;
		    }
			
		});
	}
}; jQuery.fn.QapTcha = jQuery.QapTcha.build;